﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.OrderManager
{
    public partial class MainOrderEditing : Form
    {
        #region 构造方法

        public MainOrderEditing()
        {
            InitializeComponent();
        }

        #endregion

        #region 自定义变量
        public string EditingID { get; set; }
        public string OrderNum { get; set; }
        public bool IsNew { get; set; }
        private OrderDAL dal = new OrderDAL();//数据处理
        DataTable dealerDatatable = new DataTable();//经销商
        DataTable handerDatatable = new DataTable();//拉手
        DataTable hingeDatatable = new DataTable();//铰链
        #endregion 自定义变量


        #region 按钮事件
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            MainOrder mo = new MainOrder();
            mo.PlanOrder = this.tbPlanOrder.Text.Trim();
            mo.Danhao = this.tbDanhao.Text.Trim();
            mo.Sname = this.tbSname.Text.Trim();
            mo.ContactName = this.tbContact.Text.Trim();
            mo.Phone = this.tbPhone.Text.Trim();
            mo.Address = this.tbAddress.Text.Trim();
            mo.Bzbz = this.tbBzsm.Text.Trim();
            mo.Bzfy = this.tbBzPrice.Text.Trim();//此字段需要校验么
            //二次增加
            if (!string.IsNullOrWhiteSpace(this.cbHander.Text))
            {
                mo.HanderId = this.cbHander.SelectedValue.ToString();
                mo.HanderName = this.cbHander.Text;
                mo.HanderPrice = this.tbHanderprice.Text;
                mo.HanderNum = this.tbhandernum.Text;
            }
            if (this.cbDealer.SelectedValue == null) {
                MessageBox.Show("请选择经销商，如果没有此选项，请联系管理员添加");
                return;
            }
            mo.DealerId = this.cbDealer.SelectedValue.ToString();
            mo.DealerName = this.cbDealer.Text.Trim();
            //铰链
            if (!string.IsNullOrWhiteSpace(this.cbhinge.Text))
            {
                mo.Hingeid = this.cbhinge.SelectedValue.ToString();
                mo.Hingename = this.cbhinge.Text;
                mo.Hingeprice = this.tbhingeprice.Text.Trim();
                mo.HingeNum = this.tbhingenum.Text.Trim();
            }
            mo.HingeDesc = this.tbhingedesc.Text.Trim();
            if (ValidateData(mo))
            {
                if (IsNew)
                {
                    if (!string.IsNullOrWhiteSpace(mo.HanderName))
                    {
                        mo.HanderToal = (Math.Round(Convert.ToDecimal(mo.HanderNum) * Convert.ToDecimal(mo.HanderPrice),2)).ToString();
                    }
                    else {
                        mo.HanderToal = "0";
                    }
                    if (!string.IsNullOrWhiteSpace(mo.Hingename))
                    {
                        mo.HingeTotal = (Math.Round(Convert.ToDecimal(mo.HingeNum) * Convert.ToDecimal(mo.Hingeprice), 2)).ToString();
                    }
                    else {
                        mo.HingeTotal = "0";
                    }
                    int result = dal.SaveMainOrderData(mo);
                    if (result == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                }
                else
                {
                    mo.Mid = Convert.ToInt32(EditingID);
                    if (!string.IsNullOrWhiteSpace(mo.HanderName))
                    {
                        mo.HanderToal = (Math.Round(Convert.ToDecimal(mo.HanderPrice) * Convert.ToDecimal(mo.HanderNum),2)).ToString();
                    }
                    else {
                        mo.HanderToal = "0";
                    }
                    if (!string.IsNullOrWhiteSpace(mo.Hingename))
                    {
                        mo.HingeTotal = (Math.Round(Convert.ToDecimal(mo.Hingeprice) * Convert.ToDecimal(mo.HingeNum),2)).ToString();
                    }
                    else
                    {
                        mo.HingeTotal = "0";
                    }
                    DataTable det = dal.GetDetailOrderByDanhao(OrderNum);
                    int result = dal.updateMainOrderDat(mo);
                    if (det.Rows.Count > 0)
                    {
                        result += dal.UpdateDetailOrderNum(OrderNum,mo);
                    }
                   
                    if (result >= 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                }
            }
            
        }
        //取消
        private void btnCancle_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
        //加载
        private void MainOrderEditing_Load(object sender, EventArgs e)
        {
            //经销商
            dealerDatatable=dal.GetDealer();
            DataRow d1 = dealerDatatable.NewRow();
            dealerDatatable.Rows.InsertAt(d1,0);
            this.cbDealer.DataSource = dealerDatatable;
            this.cbDealer.DisplayMember = "dname";
            this.cbDealer.ValueMember = "id";
            //拉手
            handerDatatable = dal.Gethander();
            DataRow d2 = handerDatatable.NewRow();
            handerDatatable.Rows.InsertAt(d2, 0);
            this.cbHander.DataSource = handerDatatable;
            this.cbHander.DisplayMember = "hname";
            this.cbHander.ValueMember = "hid";
            //铰链
            hingeDatatable = dal.GetHinge();
            DataRow d3 = hingeDatatable.NewRow();
            hingeDatatable.Rows.InsertAt(d3,0);
            this.cbhinge.DataSource = hingeDatatable;
            this.cbhinge.DisplayMember = "hname";
            this.cbhinge.ValueMember = "id";
           

            if (IsNew)
            {

            }
            else {
                MainOrder mo = dal.GetMainOrderById(EditingID);
                this.tbPlanOrder.Text = mo.PlanOrder;
                this.tbDanhao.Text = mo.Danhao;
                this.tbSname.Text = mo.Sname;
                this.tbPhone.Text = mo.Phone;
                this.tbContact.Text = mo.ContactName;
                this.tbAddress.Text = mo.Address;
                this.tbBzsm.Text= mo.Bzbz;
                this.tbBzPrice.Text = mo.Bzfy;
                this.tbhingedesc.Text = mo.HingeDesc;
                
                //二次修改
                this.cbDealer.Text = mo.DealerName;
                this.cbDealer.SelectedValue = string.IsNullOrWhiteSpace(mo.DealerId)?"0":mo.DealerId;
                if (!string.IsNullOrWhiteSpace(mo.HanderName)) {
                    //this.cbHander.Text = mo.HanderName;
                    this.cbHander.SelectedValue = mo.HanderId;
                    this.tbHanderprice.Text = mo.HanderPrice;
                    this.tbhandernum.Text = mo.HanderNum;
                }
                if (!string.IsNullOrWhiteSpace(mo.Hingename)) {
                    this.cbhinge.Text = mo.Hingename;
                    this.cbhinge.SelectedValue = mo.Hingeid;
                    this.tbhingeprice.Text = mo.Hingeprice;
                    this.tbhingenum.Text = mo.HingeNum;
                }
            }
        }
        #endregion 

        #region 自定义方法
        //校验字段
        public bool ValidateData(MainOrder mo) {
            bool flag = true;
            //string planOrder = this.tbPlanOrder.Text.Trim();
            //string danhao = this.tbDanhao.Text.Trim();
            //string sname = this.tbSname.Text.Trim();
            //string phone = this.tbPhone.Text.Trim();
            //string address = this.tbAddress.Text.Trim();
            //string bzfy = this.tbBzPrice.Text.Trim();
            //string bzbz = this.tbBzsm.Text.Trim();//此字段需要校验么
       
            if (string.IsNullOrWhiteSpace(mo.PlanOrder)) {
                MessageBox.Show("合同号号不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(mo.Danhao))
            {
                MessageBox.Show("单号不能为空");
                return false;
            }
            //if (string.IsNullOrWhiteSpace(mo.Sname))
            //{
            //    MessageBox.Show("客户姓名不能为空");
            //    return false;
            //}
            if (string.IsNullOrWhiteSpace(mo.Phone))
            {
                MessageBox.Show("电话不能为空");
                return false;
            }
            //if (string.IsNullOrWhiteSpace(mo.Address))
            //{
            //    MessageBox.Show("地址不能为空");
            //    return false;
            //}
            if (string.IsNullOrWhiteSpace(mo.DealerName))
            {
                MessageBox.Show("经销商不能为空");
                return false;
            }
            if (!string.IsNullOrWhiteSpace(mo.HanderName))
            {
                if (string.IsNullOrWhiteSpace(mo.HanderNum) || (Convert.ToInt32(mo.HanderNum) <= 0))
                {
                    MessageBox.Show("拉手数量不能为空或小于0");
                    return false;
                }
            }
            //if (!string.IsNullOrWhiteSpace(mo.Hingename))
            //{
            //    if (string.IsNullOrWhiteSpace(mo.HingeNum) || (Convert.ToInt32(mo.HingeNum) <= 0))
            //    {
            //        MessageBox.Show("铰链数量不能为空或小于0");
            //        return false;
            //    }
            //}
            return flag;
        }

        public void LoadData() { 
            
        }

        #endregion 自定义方法
        //包装费用数字验证
        private void tbBzPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.tbBzPrice.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }   
        }
        //订单号输入完成后校验
        private void tbDanhao_MouseLeave(object sender, EventArgs e)
        {
            if (IsNew)
            {
                string danhao = this.tbDanhao.Text.Trim();
                DataTable dt = dal.GetMainOrderByDanhao(danhao);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("单号已经存在,请确认");
                    this.tbDanhao.Text = "";
                }
            }
        }

        //经销商联动
        private void cbDealer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbDealer.SelectedIndex >0)
            {
                String id = this.cbDealer.SelectedValue.ToString();
                if (string.IsNullOrWhiteSpace(id)) return;
                DataRow[] drs = dealerDatatable.Select("id=" + id + "");
                if (drs == null)
                {
                   // this.tbAddress.Text = "";
                    this.tbPhone.Text = "";
                    this.tbContact.Text = "";
                }
                else
                {
                    this.tbPhone.Text = drs[0]["dphone"].ToString();
                    //this.tbAddress.Text = drs[0]["daddress"].ToString();
                    this.tbContact.Text = drs[0]["dcontact"].ToString();
                }
            }
            else if (this.cbDealer.SelectedIndex == 0) {
               // this.tbAddress.Text = "";
                this.tbPhone.Text = "";
                this.tbContact.Text = "";
            }


        }
        //重绘拉手下拉列表
        private void cbHander_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(cbHander.GetItemText(cbHander.Items[e.Index]).ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);  
        }
        //拉手联动
        private void cbHander_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbHander.SelectedIndex == 0) {
                this.tbHanderprice.Text = "";
                this.tbhandernum.Text = "0";
            }
            else if (cbHander.SelectedIndex > 0) {
                string hid = this.cbHander.SelectedValue.ToString();
                if (string.IsNullOrWhiteSpace(hid)) return;
                DataRow[] drs = this.handerDatatable.Select("hid="+hid);
                if (drs == null) {
                    this.tbHanderprice.Text = "";
                }
                else {
                    this.tbHanderprice.Text = drs[0]["hprice"].ToString();
                }
            }
        }

        //手机号码只能为数字
        private void tbPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            //if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            //{
            //    e.Handled = true;
            //}  
        }
        //价格只能为数字和.
        private void tbHanderprice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.tbHanderprice.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }
        //数量只能为数字
        private void tbhandernum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }

        private void cbhinge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbhinge.SelectedIndex == 0)
            {
                this.tbhingeprice.Text = "";
                this.tbhingedesc.Text = "";
                this.tbhingenum.Text = "0";
            }
            else if (cbhinge.SelectedIndex > 0)
            {
                string hid = this.cbhinge.SelectedValue.ToString();
                if (string.IsNullOrWhiteSpace(hid)) return;
                DataRow[] drs = this.hingeDatatable.Select("id=" + hid);
                if (drs == null)
                {
                    this.tbhingeprice.Text = "";
                    if (IsNew)
                    {
                        this.tbhingedesc.Text = "";
                    }
                }
                else
                {
                    this.tbhingeprice.Text = drs[0]["hprice"].ToString();
                    if (IsNew)
                    {
                        this.tbhingedesc.Text = drs[0]["hname"].ToString();
                    }
                }
            }
        }

        private void tbhangenum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }

        private void cbDealer_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(cbDealer.GetItemText(cbDealer.Items[e.Index]).ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);  
        }
    }
}
